Skip to main content

Chapter 16 - Output Values

Learn the keys and values functions and how you can use those to extract the outputs.

Basics


Simple outputs:

# 1. Output Values for Resource Group Resource
output "resource_group_id" {
description = "Resource Group ID"
# Attribute Reference
value = azurerm_resource_group.myrg.id
}
output "resource_group_name" {
description = "Resource Group Name"
# Argument Reference
value = azurerm_resource_group.myrg.name
}

# 2. Output Values for Virtual Network Resource
output "virtual_network_name" {
description = "Virtal Network Name"
value = azurerm_virtual_network.myvnet.name
#sensitive = true
}

Terraform Output


terraform output

terraform output resource_group_id terraform output -json

Sensitive Values


If you run a terraform output, sensitive values will not appear. You will need to run terraform output resource_group_id to get the output. See the sensitive toggle above.

Output with count and splat


https://developer.hashicorp.com/terraform/language/expressions/splat What is a splat expression?

# With for expression
[for o in var.list : o.id]

# With Splat Expression [*]
var.list[*].id

In the previous outputs, we can adjust this to read

# 2. Output Values - Virtual Network
output "virtual_network_name" {
description = "Virtual Network Name"
#value = azurerm_virtual_network.myvnet.name
# Can't use count.index here for multiples, so we use splat below:
value = azurerm_virtual_network.myvnet[*].name
#sensitive = true
}

If we try to use count, we end up with an error like this:

$ terraform validate

│ Error: Missing resource instance key

│ on c5-outputs.tf line 16, in output "virtual_network_name":
16: value = azurerm_virtual_network.myvnet.name

│ Because azurerm_virtual_network.myvnet has "count" set, its attributes must be
│ accessed on specific instances.

│ For example, to correlate with indices of a referring resource, use:
│ azurerm_virtual_network.myvnet[count.index]

Output with foreach and for expression


  • For
  • Keys
  • Values

for


# Output - For Loop One Input and List Output with VNET Name 
output "virtual_network_name_list_one_input" {
description = "Virtual Network Name"
value = [ for vnet in azurerm_virtual_network.myvnet: vnet.name]
}

# Output - For Loop Two Inputs, List Output which is Iterator i (var.environment)
output "virtual_network_name_list_two_inputs" {
description = "Virtual Network Name"
#value = [ for i, vnet in azurerm_virtual_network.myvnet: i]
value = [ for env, vnet in azurerm_virtual_network.myvnet: env]
}

Map outputs:

# Output - For Loop One Input and Map Output with VNET ID and VNET Name
output "virtual_network_name_map_one_input" {
description = "Virtual Network Name"
value = {for vnet in azurerm_virtual_network.myvnet: vnet.id => vnet.name}
}

# Output - For Loop Two Inputs and Map Output with Iterator env and VNET Name
output "virtual_network_name_map_two_inputs" {
description = "Virtual Network Name"
value = {for env, vnet in azurerm_virtual_network.myvnet: env => vnet.name}
}

keys


https://developer.hashicorp.com/terraform/language/functions/keys Exports the keys from that map.

> keys({a=1, c=2, d=3})
[
"a",
"c",
"d",
]
# Terraform keys() function: keys takes a map and returns a list containing the keys from that map.
output "virtual_network_name_keys_function" {
description = "Virtual Network Name - keys() Function Explore"
value = keys({for vnet in azurerm_virtual_network.myvnet: vnet.id => vnet.name})
}
# Example output
# /subscriptionid1 = vnet1 name
# /subscriptionid2 = vnet2 name
# /subscriptionid3 = vnet3 name
# /subscriptionid4 = vnet4 name

values


Exports the values from the map.

# Terraform values() function: values takes a map and returns a list containing the values of the elements in that map.
output "virtual_network_name_values_function" {
description = "Virtual Network Name - values() Function Explore"
value = values({for vnet in azurerm_virtual_network.myvnet: vnet.id => vnet.name})
}